package com.llf.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llf.Pojo.Provider;

import com.llf.Service.ProviderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ProviderController {
    @Autowired
    private ProviderServiceImpl providerService;
    @RequestMapping("/toproviderlist")
    public String toproviderlist(Model model,String proCode,String proName,
                                 Integer pageIndex,Integer pageSize)
    {


        // 为了程序的严谨性，判断非空：
        if (pageIndex == null) {
            pageIndex = 1; // 设置默认当前页
        }
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize == null) {
            pageSize = 8; // 设置默认每页显示的数据数
        }

        PageHelper.startPage(pageIndex, pageSize);
        ArrayList<Provider> page = providerService.getallProvider(proCode, proName);
        // 使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
        PageInfo<Provider> pageInfo = new PageInfo<Provider>(page);
        // 清理 ThreadLocal 存储的分页参数,保证线程安全
        PageHelper.clearPage();
        model.addAttribute("providerList",page);
        model.addAttribute("pageInfo", pageInfo);
        return "providerlist";
    }
    @GetMapping("/toPsee")
    public String tosee( Integer id,Model model){
        Provider provider = providerService.getProviderById(id);
        model.addAttribute("provider",provider);
        return "providerview";
    }
    @GetMapping("/toPupdate")
    public String toPupdate(Integer id,Model model){
        Provider provider = providerService.getProviderById(id);
        model.addAttribute("provider",provider);
        return "providermodify";
    }
    @PostMapping("/Pupdate")
    public  String update(Provider provider){
        providerService.updatePrivider(provider);
        return "redirect:/toproviderlist";
    }
    @GetMapping("/Pdelete/{id}")
    public String delete(@PathVariable("id") Integer id){
        providerService.deleteProviderById(id);
         return "redirect:/toproviderlist";
    }

    @GetMapping("/toPadd")
    public String toPadd(){
        return "provideradd";
    }

    @PostMapping("/Padd")
    public String Padd(Provider provider){
        providerService.addProvider(provider);
        return "redirect:/toproviderlist";
    }
}
